package edu.cicese.alz.swing;

import edu.cicese.alz.swing.tables.ScrollablePanel;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.border.EmptyBorder;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;

/**
 * Created by: Eduardo Quintana Contreras
 * Date: 24/07/12
 * Time: 11:35 AM
 */

public class ScrollablePane extends JScrollPane {

	protected int leftMargin = 8;

	public void initScrollablePane(Box boxContent, Color backgroundColor) {
		ScrollablePanel scrollablePanel = new ScrollablePanel(new BorderLayout());
		scrollablePanel.setScrollableWidth(ScrollablePanel.ScrollableSizeHint.STRETCH);
		scrollablePanel.setScrollableHeight(ScrollablePanel.ScrollableSizeHint.NONE);
		scrollablePanel.setScrollableBlockIncrement(
				ScrollablePanel.VERTICAL, ScrollablePanel.IncrementType.PERCENT, 200);

		boxContent.setBorder(BorderFactory.createLineBorder(backgroundColor, 4));
		scrollablePanel.add(boxContent, BorderLayout.NORTH);
		scrollablePanel.setOpaque(false);

		setViewportView(scrollablePanel);
		setBackground(backgroundColor);
		getViewport().setBackground(backgroundColor);
		setBorder(BorderFactory.createLineBorder(Color.LIGHT_GRAY, 1));
		getVerticalScrollBar().setBorder(BorderFactory.createMatteBorder(2, 0, 2, 1, backgroundColor));
		getVerticalScrollBar().setPreferredSize(new Dimension(17, Integer.MAX_VALUE));
		getHorizontalScrollBar().setBorder(BorderFactory.createMatteBorder(0, 2, 1, 2, backgroundColor));
		getHorizontalScrollBar().setPreferredSize(new Dimension(Integer.MAX_VALUE, 17));
	}

	protected JPanel createTitlePane(String title) {
		return createTitlePane(title, SwingUtil.cSectionBg);
	}

	protected JPanel createTitlePane(String title, Color titleBackground) {
		JPanel panel = new JPanel(new BorderLayout());
		JLabel lbl = new JLabel(title);
		lbl.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createMatteBorder(0, 0, 1, 0, SwingUtil.cSectionBorder),
				BorderFactory.createEmptyBorder(3, 4, 0, 1)));
		lbl.setBackground(titleBackground);
		lbl.setForeground(SwingUtil.cSectionForeground);
		lbl.setFont(SwingUtil.fontSection);
		lbl.setOpaque(true);
		panel.add(lbl);
		panel.setAlignmentX(Component.LEFT_ALIGNMENT);

		return panel;
	}

	protected JPanel createContentPane() {
		JPanel panel = new JPanel();
		panel.setBorder(BorderFactory.createEmptyBorder(0, 10, 0, 0));
		panel.setAlignmentX(Component.LEFT_ALIGNMENT);

		return panel;
	}

	protected JPanel createContentPane(Box[] boxes) {
		JPanel panel = new JPanel(new GridBagLayout());
		GridBagConstraints gbc = new GridBagConstraints();

		gbc.gridy = 0;
		gbc.weightx = 0;
		gbc.weighty = 0;
		gbc.fill = GridBagConstraints.NONE;
		gbc.anchor = GridBagConstraints.FIRST_LINE_START;

		int cols = boxes.length;
		for (int col = 0; col < cols; col++) {
			gbc.gridx = col;
			if (col == cols - 1) {
				gbc.weightx = 1;
				gbc.fill = GridBagConstraints.HORIZONTAL;
			}
			panel.add(boxes[col], gbc);
		}

		panel.setAlignmentX(Component.LEFT_ALIGNMENT);
		panel.setOpaque(false);

		return panel;
	}

	protected void setPaneContent(JPanel panel, Box[] boxes) {
		panel.setLayout(new GridBagLayout());
		GridBagConstraints gbc = new GridBagConstraints();

		gbc.gridy = 0;
		gbc.weightx = 0;
		gbc.weighty = 0;
		gbc.fill = GridBagConstraints.NONE;
		gbc.anchor = GridBagConstraints.FIRST_LINE_START;

		int cols = boxes.length;
		for (int col = 0; col < cols; col++) {
			gbc.gridx = col;
			if (col == cols - 1) {
				gbc.weightx = 1;
				gbc.fill = GridBagConstraints.HORIZONTAL;
			}
			panel.add(boxes[col], gbc);
		}
	}

	protected JPanel createEmptyPane() {
		JPanel pane = new JPanel();
		pane.setPreferredSize(new Dimension(0, 0));
		pane.setMinimumSize(pane.getPreferredSize());
		pane.setMaximumSize(pane.getPreferredSize());
		pane.setAlignmentX(Component.LEFT_ALIGNMENT);

		return pane;
	}

	protected void empty(JPanel pane) {
		pane.removeAll();
		pane.setPreferredSize(new Dimension(0, 0));
		pane.setMinimumSize(pane.getPreferredSize());
		pane.setMaximumSize(pane.getPreferredSize());
	}

	protected JPanel createCardPane() {
		JPanel panel = new JPanel(new CardLayout());
		panel.setAlignmentX(Component.LEFT_ALIGNMENT);

		return panel;
	}

	protected JPanel createCardPane(DynamicCardLayout dynamicCardLayout) {
		JPanel panel = new JPanel(dynamicCardLayout);
		panel.setAlignmentX(Component.LEFT_ALIGNMENT);

		return panel;
	}

	protected Box createHorizontalBox(Component[] components) {
		Box box = Box.createHorizontalBox();
		for (Component component : components) {
			box.add(component);
		}
		box.setAlignmentX(Component.LEFT_ALIGNMENT);

		return box;
	}

	protected Box createHorizontalColorBox(JButton button, String label) {
		return createHorizontalBox(new Component[]{
				button,
				Box.createHorizontalStrut(5),
				new JLabel(label)
		});
	}

	protected Box createVerticalBox(Component[] components, int leftBorder) {
		Box box = Box.createVerticalBox();
		for (Component component : components) {
			box.add(component);
		}
		setBoxLeftBorder(box, leftBorder);

		return box;
	}

	protected Box createVerticalBox(Component[] components) {
		return createVerticalBox(components, 8);
	}

	protected Icon getIcon(String iconName) {
		return new ImageIcon(getClass().getClassLoader().getResource("icons/" + iconName + ".png"));
	}

	protected void setBoxLeftBorder(Box box, int left) {
		box.setBorder(new EmptyBorder(0, left, 0, 0));
	}

	protected void setTextFieldErrorFont(JNonEmptyTextField textField, boolean error) {
		textField.setError(error);
		if (error) {
			textField.setForeground(Color.RED);
			//			textField.setBackground(SwingUtil.cTextFieldError);
			//			textField.setBorder(SwingUtil.borderTextFieldError);
		} else {
			textField.setForeground(Color.BLACK);
			//			textField.setBackground(Color.WHITE);
			//			textField.setBorder(SwingUtil.borderTextField);
		}
	}


	protected class EmptyTextListener implements DocumentListener {
		private JNonEmptyTextField textField;

		public EmptyTextListener(JNonEmptyTextField textField) {
			this.textField = textField;
		}

		@Override
		public void insertUpdate(DocumentEvent e) {
			checkText();
		}

		@Override
		public void removeUpdate(DocumentEvent e) {
			checkText();
		}

		@Override
		public void changedUpdate(DocumentEvent e) {
			checkText();
		}

		public void checkText() {
			SwingUtil.checkEmptyTextField(textField, true);
		}
	}
}
